<template>
    <div class="app-container">
        <uc-table url="admin/equipment/getInspectionPage" name="设备巡检管理" ref="inspectionTable" pageable table-border :search-form="searchForm">
            <template slot="searchForm">
                <el-form size="mini" :inline="true" :model="searchForm" class="demo-form-inline filter-container">
                    <el-form-item>
                        <el-select v-model="searchForm.equipmentId" placeholder="请选择设备" style="width: 100%" clearable @clear="clearInput1()" >
                            <el-option v-for="(item,index) in equipmentList" :key="index"  :value="item.ucId" :label="item.name"></el-option>
                        </el-select>
                    </el-form-item>
                    <el-form-item v-if="enterPriseHidden">
                        <el-select v-model="searchForm.enterpriseId" placeholder="请选择主体"  clearable @clear="clearInput2()">
                            <el-option v-for="(item,index) in enterpriseDominantList" :key="index"  :value="item.ucId" :label="item.name"></el-option>
                        </el-select>
                    </el-form-item>
                    <!--搜索按钮--新增按钮-->
                    <el-form-item>
                        <el-button type="primary" icon="el-icon-search" class="filter-item" @click="handleSearch()">查询</el-button>
                        <el-button type="success" icon="el-icon-plus" class="filter-item" @click="handleAdd()">新 增</el-button>
                    </el-form-item>
                </el-form>
            </template>
            <template slot="columns">
                <el-table-column prop="equipmentName" label="设备名称" align="left" show-overflow-tooltip></el-table-column>
                <el-table-column prop="inspectionUserName" label="巡检人员" align="left" show-overflow-tooltip ></el-table-column>
                <el-table-column prop="inspectionTime"  label="巡检时间" align="left" show-overflow-tooltip>
                    <template slot-scope="scope">
                        {{scope.row.inspectionTime |dateFormat}}
                    </template>
                </el-table-column>
                <el-table-column prop="equipmentStatus" label="设备状态" align="left">
                    <template slot-scope="scope">
                        {{scope.row.equipmentStatus | equipmentStatus}}
                    </template>
                </el-table-column>
                <el-table-column prop="inspectionRemark" label="巡检描述" align="left" show-overflow-tooltip></el-table-column>
                <el-table-column prop="address" label="操作" width="220" align="left" class-name="small-padding fixed-width">
                    <template slot-scope="scope">
                        <!--编辑按钮-->
                        <el-button size="mini" icon="el-icon-edit" type="primary" @click="handleEdit(scope.row)">编辑</el-button>
                        <el-button size="mini" icon="el-icon-info" type="success" @click="handleInfo(scope.row)">详情</el-button>
                        <el-button size="mini" type="danger" icon="el-icon-delete" :loading="deleteLoading" @click="handleDelete(scope.row)">删除</el-button>
                    </template>
                </el-table-column>
            </template>
        </uc-table>
    </div>
</template>

<script>
    import UcTable from "@/components/UcTable/index.vue";
    import {deleteInspection,getEquipmentList} from "@/api/equipment";
    import {getInEnterpriseList} from "@/api/enterprise";
    import {getInEnterpriseDominantList} from "@/api/enterpriseDominant";
    export default {
        name:'inspectionTable',
        components: {UcTable},
        data(){
            return{
                searchForm:{
                    deleted:false,
                    equipmentId:this.$route.query.equipmentId
                },
                deleteLoading:false,
                InspectionList:{
                    ucId:'',
                },
                equipmentList:[],
                enterpriseDominantList:[],
            }
        },
      computed:{
        enterPriseHidden(){
          let roleCode = this.$store.getters.roleCode;
          return roleCode=="ADMIN"||roleCode=="XT-ADMIN"||roleCode=="ZD-ADMIN"||roleCode=="DD-ADMIN"||roleCode=="JW-ADMIN"
        }
      },
        created() {
            this.getEquipmentList()
            this.getInEnterpriseDominantList()
        },
        methods:{
            handleSearch(){
                this.$refs.inspectionTable.getTableData(1)
            },
            handleAdd(){
                this.$router.push("/equipment/inspectionEdit/0")
            },
            handleEdit(val){
                this.$router.push("/equipment/inspectionEdit/"+val.ucId)
            },
            handleInfo(val){
                this.$router.push("/equipment/inspectionInfo/"+val.ucId)
            },
            clearInput1(){
                //清除输入框
                this.searchForm.equipmentId = null
                this.handleSearch()
            },
            clearInput2(){
                //清除输入框
                this.searchForm.enterpriseId = null
                this.handleSearch()
            },
            handleDelete(val){
                this.$confirm('您确定要执行删除这条巡检信息吗？', '提醒信息', {
                    confirmButtonText: '确定',
                    cancelButtonText: '取消',
                    type: 'warning'
                }).then(() => {
                    this.deleteLoading = true
                    deleteInspection(val).then(res=>{
                        this.deleteLoading = false
                        this.$notify.success({
                            title: '成功',
                            message: "删除设备成功"
                        })
                        this.handleSearch()
                    }).catch(res=>{
                        this.deleteLoading = false
                        this.$notify.error({
                            title: '失败',
                            message: res.msg
                        })
                    })
                }).catch(() => {
                    this.$message({
                        type: 'info',
                        message: '已取消删除'
                    });
                });
            },
            getEquipmentList(){
                getEquipmentList({deleted:false}).then(res=>{
                    this.equipmentList = res.data
                }).catch(error=>{

                })
            },
            getInEnterpriseDominantList(){
                getInEnterpriseDominantList({deleted:false}).then(res=>{
                    this.enterpriseDominantList = res.data
                }).catch(error=>{

                })
            }

        },
        filters:{
            equipmentStatus(val){
                let  equipmentStatus="";
                switch (val){
                    case 0:
                        equipmentStatus = "正常";
                        break;
                    case 1:
                        equipmentStatus= "损坏";
                        break;
                    case 2:
                        equipmentStatus= "停机";
                        break;
                    default:
                        break;
                }

                return equipmentStatus;
            }
        }
    }
</script>